package link.ten_fei.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import link.ten_fei.Bean.Doc_User;
import link.ten_fei.util.ConnectionFactory;

public class UserLoginDaoImpl implements UserLoginDao {

	@Override
	public Doc_User findByNamAndPass(String doc_username, String doc_password) {
		// 1.获取数据库连接
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement preparedStatement = null;
		ResultSet rs = null;
		try {
			// 2.准备sql语句
			String sql = "select id, doc_name,doc_username,doc_password,doc_judge from doc_user where doc_username=? and doc_password=?";
			// 3.获取连接通路
			preparedStatement = connection.prepareStatement(sql);
			// 4.索引
			preparedStatement.setString(1, doc_username);
			preparedStatement.setString(2, doc_password);
			// 5.执行sql语句,返回查询的结果,封装在rs中
			rs = preparedStatement.executeQuery();

			// 查询的结果包含表头信息
			while (rs.next()) {
				Doc_User doc_User = new Doc_User();
				// 每一行的数据封装在一个实体bean中，根据字段获取字段值，该字段是什么类型就get什么类型
				doc_User.setId(rs.getInt("id"));
				doc_User.setDoc_name(rs.getString("doc_name"));
				doc_User.setDoc_username(rs.getString("doc_username"));
				doc_User.setDoc_password(rs.getString("doc_password"));
				doc_User.setDoc_judge(rs.getInt("doc_judge"));
				return doc_User;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(connection, preparedStatement, rs);
		}
		return null;
	}

}
